package com.burgeon.backend.Rest.coupon.xe;
/**
 * 创建优惠券
 */

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.entity.Coupon;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.HashMap;

public class CreateCoupon extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public CreateCoupon(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符
     */

    public String getData(Coupon coupon) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        System.err.println(Data(coupon));
        String str = RESULT(Data(coupon), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");

        if (!"0".equals(code)) {
            throw new Exception(message);
        }
        return new JSONArray(str).getJSONObject(0).getString("objectid");
    }

    private MultiValueMap Data(Coupon coupon) {

        HashMap params = new HashMap();

        params.put("TICKETNO","'"+coupon.getCode()+"'");//券号
        if (!"0".equals(coupon.getVipIntegral())) {
            params.put("C_VIPINTEGRALADJ_ID__DOCNO", "'"+coupon.getVipIntegral()+"'");//积分调整单id
        }
      //params.put("TICKET_TYPE",coupon.getType());//类型

        params.put("C_VIP_ID__CARDNO","'"+coupon.getVipCode()+"'");
        params.put("CHECKNO","'"+coupon.getCode()+"'");//校验码
        params.put("PARVALUE","'"+coupon.getMoney()+"'");//面值
        params.put("DATEBEG","'"+coupon.getDatebeg()+"'");//起始日期
        params.put("DATEEND","'"+coupon.getDateend()+"'");//截止日期
        params.put("REMARK","小程序线上商城");//备注

        params.put("table", "C_V_TDEFTICKET");//表名


        return DATA("ObjectCreate", params, account, password);
    }

    public static void main(String[] args) {
        CreateCoupon g = new CreateCoupon("139.129.93.47:55321", "nea@burgeon.com.cn", "burgeon55321");
        try {
            Coupon coupon = new Coupon();
            coupon.setVipCode("17732141036");
            coupon.setVipIntegral("0");
            coupon.setCode("26699x");
            coupon.setType("现金券");
            coupon.setCheckno("asdw1we1121");
            coupon.setMoney("123");
            coupon.setDatebeg("20191112");
            coupon.setDateend("20191130");
            coupon.setVipIntegral("IJ2012100000100");
            g.getData(coupon);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
